<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
      <el-form-item label="表单名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入表单名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['service:form:add']">新增表单</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table border v-loading="loading" :data="list">
      <el-table-column prop="id" label="表单ID" show-overflow-tooltip v-if="columns[0].visible" />
      <el-table-column prop="resourceName" label="表单名称" v-if="columns[1].visible" show-overflow-tooltip  width="150" />
      <el-table-column prop="key" label="表单键" v-if="columns[2].visible" show-overflow-tooltip />
      <el-table-column prop="version" label="版本号" v-if="columns[3].visible" show-overflow-tooltip width="100" />
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['service:form:query']">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['service:form:edit']">编辑</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['service:form:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size" @pagination="getList" />
  </div>
</template>

<script setup name="Process">
import { listForm, delProcess} from "@/api/service/process"

const router = useRouter()
const route = useRoute()
const { proxy } = getCurrentInstance()

const uniqueId = ref("")
const showSearch = ref(true)
const columns = ref([
  { key: 0, label: `表单ID`, visible: true, disabled: true },
  { key: 1, label: `表单名称`, visible: true },
  { key: 2, label: `表单键`, visible: true },
  { key: 3, label: `创建时间`, visible: true }
])
const loading = ref(true)
const list = ref([])
const total = ref(0)
const queryParams = ref({
  current: 1,
  size: 10,
  name: undefined,
})

onActivated(() => {
  const time = route.query.t
  if (time != null && time != uniqueId.value) {
    uniqueId.value = time
    queryParams.value.current = Number(route.query.current)
    getList()
  }
})

/** 查询角色列表 */
function getList() {
  loading.value = true
  listForm(queryParams.value).then(response => {
    const result = response.result || {}
    list.value = result.records || []
    total.value = Number(result.total)
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除表单名称为"' + row.resourceName + '"的表单?').then(function () {
    return delProcess(row.deploymentId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function handleAdd() {
  const query = { current: queryParams.value.current }
  router.push({ path: "/service/form-data/add", query })
}

function handleUpdate(row) {
  const query = { current: queryParams.value.current }
  router.push({ path: "/service/form-data/edit/" + row.deploymentId, query })
}

function handleView(row) {
  const query = { current: queryParams.value.current }
  router.push({ path: "/service/form-data/view/" + row.deploymentId, query })
}

getList()
</script>